@extends('layout.layer')

@section('layout.content')
  <div class="row">
    <div class="col-xs-12">
      <form id="act-create" autocomplete="off">
        <input type="hidden" name="tid" value="{{$topic->id}}">
        <div class="form-group row">
          <label class="col-xs-12 control-label no-padding-right" for="type"> <b><span class="red align-middle">*</span> 活动名称：</b>
          </label>
          <div class="col-xs-12">
            <input type="text" class="form-control" id="name" name="name" value="{{$topic->name}}" placeholder="活动名称"/>
          </div>
        </div>
        
        <div class="form-group row">
          <label class="col-xs-12 control-label" for="name"> <b><span class="red align-middle">*</span> 题库类型：</b>
          </label>
          <div class="col-xs-12">
            <div class="radio">
                <label class="inline no-padding-left">
                  <input name="type" type="checkbox" value="1" class="ace ace-checkbox-2 input-lg" {{$topic->type==1?'checked':''}}>
                  <span class="lbl"> 选择题</span>
                </label>
                <label class="inline">
                  <input name="type" type="checkbox" value="2" class="ace ace-checkbox-2 input-lg" {{$topic->type==2?'checked':''}}>
                  <span class="lbl"> 填空题</span>
                </label>
              </div>
          </div>
        </div>

        <div class="form-group row">
            <label class="col-xs-12 control-label" for="status"> <b><span class="red align-middle">*</span> 状态：</b>
            </label>
            <div class="col-xs-12">
                <div class="radio">
                  <label class="inline no-padding-left">
                    <input name="status" type="checkbox" value="0" class="ace ace-checkbox-2 input-lg" {{$topic->status==0?'checked':''}}>
                    <span class="lbl"> 未开始</span>
                  </label>
                  <label class="inline">
                    <input name="status" type="checkbox" value="1" class="ace ace-checkbox-2 input-lg" {{$topic->status==1?'checked':''}}>
                    <span class="lbl"> 进行中</span>
                  </label>
                  <label class="inline">
                    <input name="status" type="checkbox" value="2" class="ace ace-checkbox-2 input-lg" {{$topic->status==2?'checked':''}}>
                    <span class="lbl"> 已结束</span>
                  </label>
                </div>
            </div>
        </div>
        <div class="form-group row">
            <label class="col-xs-12 control-label" for="status"> <b><span class="red align-middle">*</span> 简介：</b>
            </label>
            <div class="col-xs-12">
              <textarea rows="4" class="form-control" name="desc" placeholder="活动简介">{{$topic->desc}}</textarea>
            </div>
        </div>

        <div class="form-group row">
            <label class="col-xs-12 control-label"> <b>UUID：</b>
            </label>       
            <label class="col-xs-12 control-label"> {{$topic->token}}</label>      
        </div>

        <div class="space-16"></div>
        <div class="form-group">
          <div class="col-xs-12 center">
            <button type="submit" class="btn btn-primary" style="margin-right:15px;">
              <i class="ace-icon fa fa-check"></i>
              确定
            </button>

            <button type="button" class="btn btn-light">
              <i class="ace-icon fa fa-times"></i>
              取消
            </button>
          </div>
        </div>

      </form>
    </div>
  </div>
@stop

@section('javascript')

  <script src="//cdn.bootcss.com/jquery-validate/1.19.1/jquery.validate.min.js"></script>
  <script src="{{URL::asset('assets/js/jquery-validate-methods.js')}}"></script>
  <script src="//cdn.bootcss.com/jqueryui/1.12.1/jquery-ui.min.js"></script>


  <script type="text/javascript">
      $(function () {

          $("input:checkbox[name='type']").on('click', function (e) {
              $("input:checkbox[name='type']").not(this).attr("checked", false);
          });

          $("input:checkbox[name='status']").on('click', function (e) {
              $("input:checkbox[name='status']").not(this).attr("checked", false);
          });

          $('.btn-light').on('click', function(e) {  
            //parent.location.reload();
            //var index = parent.layer.getFrameIndex(window.name); 
            parent.layer.close(parent.layer.index);            
          });
          /**
           表单前端验证
           */
          $("#act-create").validate({
              rules: {
                  name: {required: true},
                  type: {required: true},
                  status: {required: true},
              },
              messages: {
                  name: {required: "请填写活动名称"},
                  type: {required: "请选择题库类型"},
                  status: {digits: "请选择活动状态"}
              },


              /* 失去焦点时不验证 */
              onfocusout: false,
              onkeyup: false,
              errorElement: 'div',
              errorClass: 'help-block',

              success: function (e) {
                  $(e).closest('.form-group').removeClass('has-error');//.addClass('has-info');
                  $(e).remove();
              },

              showErrors: function (errorMap, errorList) {
                  $.each(errorList, function (i, v) {
                      layer.tips(v.message, v.element, {tips: [3,'#dd0000'], time: 2000});
                      return false;
                  });

              },

              submitHandler: function (form) {
                var tid = $("input[name='tid']").val();
                var uri = '/act/update/'+tid;
                  $.post(uri, {
                      '_token': $("input[name='_token']").attr('value'),
                      'name': $("input[name='name']").val(),
                      'type': $("input[name='type']:checked").val(),
                      'status': $("input[name='status']:checked").val(),
                      'desc': $("textarea[name='desc']").val(),
                      'is_submit': true
                  }, function (data) {
                      console.log(data);
                      layer.msg(data.message, {time: 2000,shift: -1}, function () {
                          if (data.status === true) {
                              parent.location.reload();
                              parent.layer.close(parent.layer.index);
                          }
                      });

                  }, 'json').error(function (data) {
                      layer.msg(data.responseJSON.message);
                  });
              }


          });

      });
  </script>
@stop